package me.tuyou.stream.window;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * 计数窗口demo
 */
public class CountWindowTest {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<Tuple2<String, Integer>> input = env.fromElements(
                Tuple2.of("s1", 1),
                Tuple2.of("s1", 2),
                Tuple2.of("s1", 3),
                Tuple2.of("s2", 4),
                Tuple2.of("s2", 5),
                Tuple2.of("s2", 6),
                Tuple2.of("s3", 7),
                Tuple2.of("s3", 8),
                Tuple2.of("s3", 9)
        );
        input.keyBy(0)
//                .countWindow(3)  // 滚动窗口
                .countWindow(3, 1) // 滑动窗口
                .sum(1)
                .print();
        env.execute();
    }
}
